/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.alarm;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.alarm.EleSourceAlarmGroup;
import com.thinkgem.jeesite.modules.source.service.alarm.EleSourceAlarmGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 警告资源管理Controller
 * @author ZC
 * @version 2017-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/source/alarm/eleSourceAlarmGroup")
public class EleSourceAlarmGroupController extends BaseController {

	@Autowired
	private EleSourceAlarmGroupService eleSourceAlarmGroupService;
	
	@ModelAttribute
	public EleSourceAlarmGroup get(@RequestParam(required=false) String id) {
		EleSourceAlarmGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceAlarmGroupService.get(id);
		}
		if (entity == null){
			entity = new EleSourceAlarmGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceAlarmGroup eleSourceAlarmGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleSourceAlarmGroup> page = eleSourceAlarmGroupService.findPage(new Page<EleSourceAlarmGroup>(request, response), eleSourceAlarmGroup); 
		model.addAttribute("page", page);
		return "modules/source/alarm/eleSourceAlarmGroupList";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:view")
	@RequestMapping(value = "form")
	public String form(EleSourceAlarmGroup eleSourceAlarmGroup, Model model) {
		model.addAttribute("eleSourceAlarmGroup", eleSourceAlarmGroup);
		return "modules/source/alarm/eleSourceAlarmGroupForm";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceAlarmGroup eleSourceAlarmGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleSourceAlarmGroup)){
			return form(eleSourceAlarmGroup, model);
		}
		eleSourceAlarmGroupService.save(eleSourceAlarmGroup);
		addMessage(redirectAttributes, "保存警告资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/alarm/eleSourceAlarmGroup/?repage";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceAlarmGroup eleSourceAlarmGroup, RedirectAttributes redirectAttributes) {
		eleSourceAlarmGroupService.delete(eleSourceAlarmGroup);
		addMessage(redirectAttributes, "删除警告资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/alarm/eleSourceAlarmGroup/?repage";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:check")
	@RequestMapping(value = "check")
	public String check(EleSourceAlarmGroup eleSourceAlarmGroup, RedirectAttributes redirectAttributes) {
		eleSourceAlarmGroupService.check(eleSourceAlarmGroup);
		addMessage(redirectAttributes, "审核背景资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/alarm/eleSourceAlarmGroup/?repage";
	}
}